package com.seassoon.sdkservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mzlion.easyokhttp.HttpClient;
import com.seassoon.sdkservice.entity.*;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class SiXiangDataPush {


    @Value("${pushURL}")
    private String apiPush;
    @Value("${approvalInfoURL}")
    private String approvalInfoURL;
    /**
     * 调取四象接口
     */
    public JSONObject queryA(String itemInternalNo){
//        String url = apiPush;
        String s_url = apiPush+"/api/sixiang/savePush";
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("itemInternalNo", itemInternalNo);
        String Respondstr= HttpClient.post(s_url).param(paramMap).execute().asString();
        JSONObject jsonObject = JSON.parseObject(Respondstr);

        return jsonObject;
    }

    /**
     * 帮办上（fieldjson和stepsjson）
     * 将两大json转化成实体类即可  <- 错误说法
     * 实体类是因为可视化
     */
    public PushBangBanResult allToBean(String sid){
        JSONArray fieldWithChildrenJsonList = JSON.parseObject(queryA(sid).getString("data")).getJSONArray("fieldWithChildren"); // 字段
        JSONArray stepsJsonList = JSON.parseObject(queryA(sid).getString("data")).getJSONArray("steps");  // 步骤
        JSONArray templatePagesResultsJsonList = JSON.parseObject(queryA(sid).getString("data")).getJSONArray("templatePagesResults"); // 材料
        PushBangBanResult pushBangBanResultList = new PushBangBanResult();
//        pushBangBanResultList.setFieldWithChildren(JSON.parseArray(JSONObject.toJSONString(fieldWithChildrenJsonList), FieldWithChildren.class));
//        pushBangBanResultList.setSteps(JSON.parseArray(JSONObject.toJSONString(stepsJsonList), Steps.class)); //保存步骤页面
        JSONObject data = JSONObject.parseObject(queryA(sid).getString("data"));
        pushBangBanResultList.setFieldsJson(null == data.getJSONArray("fieldWithChildren") ? null : data.getJSONArray("fieldWithChildren").toJSONString());
        pushBangBanResultList.setStepJson(null == data.getJSONArray("steps") ? null : data.getJSONArray("steps").toJSONString() );
        //材料信息
        List<HtmlTemplatePageEntity> htmlTemplatePageEntities = new ArrayList<>(); // 模板单页信息
        List<ApprovalMaterial> approvalMaterials = new ArrayList<>();  // 事项材料信息
        templatePagesResultsJsonList.stream().forEach(temp -> {

            ApprovalMaterial material = new ApprovalMaterial();
            JSONObject t1 = JSON.parseObject(temp.toString()).getJSONObject("template");
            material.setNotes(t1.getString("notes"));
            material.setScript(t1.getString("script"));
            material.setDocumentSeq(t1.getString("documentSeq"));
            material.setSourceOfDocument(t1.getString("produceSource"));
            material.setDocumentName(t1.getString("materialName"));
            material.setDocxTemplateName(t1.getString("docxTemplateName"));

            // 单页模板
            JSONArray tpageList = JSON.parseObject(temp.toString()).getJSONArray("templatePagesList");
            tpageList.stream().forEach(page -> {
                HtmlTemplatePageEntity template = new HtmlTemplatePageEntity();
                template.setSid(sid);
                template.setFileName(t1.getString("docxTemplateName"));
                template.setName(t1.getString("materialName"));
                if ("0".equals(JSON.parseObject(page.toString()).getString("isTable"))){
                    template.setPadding("text");
                }else{
                    template.setPadding("table");
                }

                template.setOrient(JSON.parseObject(page.toString()).getString("orient"));
                template.setPageNum(JSONObject.parseObject(page.toString()).getInteger("pageNum"));
                String htmlPath = "/" + sid + "/" + t1.getString("docxTemplateName") + "_page" + JSONObject.parseObject(page.toString()).getInteger("pageNum") + ".html";
                //htmlPath: "/310115-422-02/31011542202_1_tpl_page1.html"
                template.setHtmlPath(htmlPath);
                template.setContentCss(JSON.parseObject(page.toString()).getString("contentCss"));
                template.setHtml(JSONObject.parseObject(page.toString()).getString("htmlContent"));
                htmlTemplatePageEntities.add(template);
            });

            approvalMaterials.add(material);
//            htmlTemplatePageEntities.add(template);
        });
        pushBangBanResultList.setApprovalMaterials(approvalMaterials);
        pushBangBanResultList.setHtmlTemplatePageEntities(htmlTemplatePageEntities);
        return pushBangBanResultList;
    }

    /**
     * 调取四象事项信息接口(无大项名称)
     */
    public JSONObject approvalItemInfo(String itemInternalNo){
//        String url = apiPush;
        String s_url = approvalInfoURL+"/api/sixiang/getByItemInternalNo";
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("itemInternalNo", itemInternalNo);
        String Respondstr= HttpClient.get(s_url).queryString("itemInternalNo",itemInternalNo).execute().asString();
        JSONObject jsonObject = JSON.parseObject(Respondstr);
        return jsonObject;
    }

    /**
     * 调取四象事项信息接口(含大项名称)
     */
    public JSONObject approvalItemInfoAndApprovalName(String itemInternalNo){
        String s_url = approvalInfoURL+"/api/sixiang/getApprovalItemAndApprovalNameByItemInternalNo";
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("itemInternalNo", itemInternalNo);
        String Respondstr= HttpClient.get(s_url).queryString("itemInternalNo",itemInternalNo).execute().asString();
        JSONObject jsonObject = JSON.parseObject(Respondstr);
        return jsonObject;
    }

}
